package com.salts.controller;

import com.salts.domain.dto.Result;
import com.salts.domain.dto.UpdateOrderAddressDto;
import com.salts.domain.po.OrderInfo;
import com.salts.domain.po.UserAddress;
import com.salts.domain.po.UserInfo;
import com.salts.domain.vo.DisplayOrderVo;
import com.salts.domain.vo.OrderDetailVo;
import com.salts.domain.vo.OrderVo;
import com.salts.service.MineService;
import com.salts.service.OrderFunctionService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单模块 /orderFunction
 */
@RestController
@RequestMapping("/orderFunction")
public class OrderFunctionController {
    @Autowired
    OrderFunctionService orderFunctionService;
    @Autowired
    MineService mineService;

    /**
     * 获取基本订单信息 /getOrderInfo -Post
     * */
    @RequestMapping("/getOrderInfo")
    public Result getOrderInfo(@RequestBody Map<String,Integer> paramMap, UserInfo condition){
        Integer userId = paramMap.get("userId");
        Integer orderId = paramMap.get("orderId");
        condition.setId(userId);
        OrderVo orderInfo = orderFunctionService.getOrderInfo(condition, orderId);
        return Result.ok(orderInfo);
    }//基本订单信息

    /**
     * 订单商品列表 /getOrderList -Post
     * @param 入参示例：{
     *   "userId":3,
     *   "state":0
     * }
     * @param userId 用户id
     * @param state 订单状态：-2已取消 -1已退款 0未付款 1正在开单 2出库中 3待配送 4配送中 5已完成
     */
    @RequestMapping("/getOrderList")
    public Result getOrderList(@RequestBody Map<String,Integer> paramMap, UserInfo userInfo){
        Integer id = paramMap.get("userId");
        Integer state = paramMap.get("state");
        userInfo.setId(id);
        HashMap<String, Object> returnMap = new HashMap<>();
        ArrayList<Map> returnList = new ArrayList<>();
        HashMap<String, List<DisplayOrderVo>> orderList = orderFunctionService.getOrderList(userInfo, state);
        for (Map.Entry<String, List<DisplayOrderVo>> stringListEntry : orderList.entrySet()) {
            returnMap.put("orderNo",stringListEntry.getKey());
            returnMap.put("orderData",stringListEntry.getValue());
            returnList.add(returnMap);
        }

        return Result.ok(returnList);
    }//订单商品列表

    /**
     * 更改订单状态 /updateOrderState -Post
     */
    @RequestMapping("/updateOrderState")
    public Result updateOrderState(@RequestBody Map<String,Integer> paramMap){
        Integer orderId = paramMap.get("orderId");
        Integer state = paramMap.get("state");
        Boolean b = orderFunctionService.updateOrderState(orderId, state);
        return Result.ok(b);
    }//更改订单状态

    /**
     * 修改订单地址 /updateOrderAddress -Post
     */
    //todo 记得加入查找订单后确认状态只能为0，1，2之后才能改
    @PostMapping("/updateOrderAddress")
    public Result updateOrderAddress(@RequestBody UpdateOrderAddressDto updateOrderAddressDto,UserAddress condition){
        condition = updateOrderAddressDto.getUserAddress();
        Integer orderId = updateOrderAddressDto.getOrderId();
        Boolean b = orderFunctionService.updateOrderAddress(orderId, condition);
        return Result.ok(b);
    }//修改订单地址
    //支付信息

    //物流信息暂时不做

    /**
     * 获取订单详情+物流单号
     * @param orderInfo
     * @return
     */
    @PostMapping("/orderDetail")
    public Result getOrderDetail(@RequestBody OrderInfo orderInfo){
        OrderDetailVo orderDetailVo = mineService.queryOrderDetail(orderInfo);
        return Result.ok(orderDetailVo);
    }


}
